package com.totcy.atom.utils;

import android.Manifest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Environment;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * description: 保存图片工具类
 * author: Totcy
 * date: 2017/11/7 10:29
 * update: 2017/11/7
 */

public class SaveImageUtils {

    /**
     *  Glide 加载图片保存到本地
     *
     * @param activity
     * @param url
     */
    public static void saveImage(final Activity activity, final String url){
        new RxPermissions(activity)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if(aBoolean){
                            Glide.with(activity)
                                    .asBitmap()
                                    .load(url)
                                    .into(new SimpleTarget<Bitmap>() {
                                        @Override
                                        public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                                            saveBitmap(activity,"IMG"+System.currentTimeMillis()+".jpg",bitmap);
                                        }
                                    });
                        }
                    }
                });
    }


    // 保存图片到手机指定目录
    private static void saveBitmap(Activity activity,String imgName, Bitmap bitmap) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String filePath ;
            FileOutputStream fos = null;
            //输入
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] datas = baos.toByteArray();

            try {
                filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/MyImg";
                File imgDir = new File(filePath);
                if (!imgDir.exists()) {
                    imgDir.mkdirs();
                }
                imgName = filePath + "/" + imgName;
                fos = new FileOutputStream(imgName);
                fos.write(datas);
                Toast.makeText(activity, "图片已保存到" + filePath, Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Toast.makeText(activity, "请检查SD卡是否可用", Toast.LENGTH_SHORT).show();
        }
    }

}
